package cn.jolyoulu.adminweb.service.impl;


import cn.jolyoulu.adminweb.entity.bo.form.SysMenuFormBO;
import cn.jolyoulu.adminweb.entity.vo.SysMenuVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.jolyoulu.adminweb.service.ApiSysMenuService;
import cn.jolyoulu.cmnservice.entity.po.SysMenu;
import cn.jolyoulu.cmnservice.serivce.SysMenuService;
import cn.jolyoulu.common.web.enums.GlobalExpType;
import cn.jolyoulu.common.web.excption.GlobalException;

import java.util.List;

/**
 * @Author LuZhouJin
 * @Date 2023/3/16
 */
@Slf4j
@Service
public class ApiSysMenuServiceImpl implements ApiSysMenuService {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private ApiSysMenuService apiSysMenuService;

    @Override
    public List<SysMenuVO> tree() {
        List<SysMenuVO> tree = sysMenuService.selectTree("0", SysMenuVO::new);
        return tree;
    }

    @Override
    public SysMenuVO getById(String id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        return new SysMenuVO(sysMenu);
    }

    @Override
    @Transactional
    public void add(SysMenuFormBO bo) {
        //添加检查
        apiSysMenuService.addCheck(bo);
        //添加根节点
        if (StringUtils.isBlank(bo.getParentId())){
            sysMenuService.addRootMenu(SysMenuFormBO.toAddMenuDTO(bo));
        //添加子节点
        }else {
            sysMenuService.addLeafMenu(SysMenuFormBO.toAddMenuDTO(bo));
        }
    }

    public void addCheck(SysMenuFormBO bo) {
        Integer menuType = bo.getMenuType();
        if (menuType < 1 || menuType > 2){
            throw new GlobalException(GlobalExpType.PARAM_ERROR);
        }
        switch (menuType){
            case 1:
                if (StringUtils.isBlank(bo.getPath())){
                    throw new GlobalException(GlobalExpType.PARAM_ERROR);
                }
                if (StringUtils.isBlank(bo.getComponent())){
                    throw new GlobalException(GlobalExpType.PARAM_ERROR);
                }
                break;
            case 2:
                if (StringUtils.isBlank(bo.getPermission())){
                    throw new GlobalException(GlobalExpType.PARAM_ERROR);
                }
                break;
        }
    }

    @Override
    @Transactional
    public void update(SysMenuFormBO bo) {
        SysMenu menu = sysMenuService.getById(bo.getId());
        if (bo.getParentId().equals(menu.getId())) {//父级如是自己不处理(否则栈溢出)
            bo.setParentId(null);
        }
        //判断是否修改了父级编号
        if (StringUtils.isNotBlank(bo.getParentId())  //父级非空
                && !menu.getParentId().equals(bo.getParentId())){ //父级有变化
            sysMenuService.updateParent(bo.getId(), bo.getParentId());
        }
        SysMenu update = new SysMenu();
        BeanUtils.copyProperties(bo,update);
        sysMenuService.updateById(update);
    }

    @Override
    @Transactional
    public void delByIds(List<String> ids) {
        for (String id : ids) {
            sysMenuService.delById(id);
        }
    }
}
